package com.jaygoo.wavelineview.slice;

import static ohos.bundle.AbilityInfo.DisplayOrientation.LANDSCAPE;
import static ohos.bundle.AbilityInfo.DisplayOrientation.PORTRAIT;

import jaygoo.widget.wlv.WaveLineView;
import com.jaygoo.wavelineview.MyApplication;
import com.jaygoo.wavelineview.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;

import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    private static final int TWO = 2;
    private static final int ONE = 1;
    private static final int LABELNO = 0x000110;
    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,LABELNO, "CircleImageView");
    private static HiLogLabel label1 = new HiLogLabel(HiLog.LOG_APP, LABELNO, "mlvMainAbility");

    private WaveLineView waveLineView;

    private Button startBtn = null;
    private Button stopBtn = null;
    private Button leakTestBtn = null;
    private DirectionalLayout titlebardirLayout = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#f34050B5"));
        titlebardirLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_titlebar_dirLayout);
        int width = getDisplayWidthInPx(getContext());
        int height = getDisplayHeightInPx(getContext());
        waveLineView = (WaveLineView) findComponentById(ResourceTable.Id_waveLine);
        if (MyApplication.getScreenOrientation() == TWO) {
            HiLog.info(label1,"ScreenOrientation  width: " + width + ", height: " + height);
            int mWidth;
            if (width < height) {
                mWidth = height;
            } else {
                mWidth = width;
            }
            waveLineView.setWidth(mWidth);
        }

        if (MyApplication.getScreenOrientation() == ONE) {
            HiLog.info(label1,"ScreenOrientation  ver width: " + width + ", height: " + height);
            waveLineView.setWidth(width);
        }

        startBtn = (Button) findComponentById(ResourceTable.Id_button_startBtn);
        stopBtn = (Button) findComponentById(ResourceTable.Id_stopBtn);
        leakTestBtn = (Button) findComponentById(ResourceTable.Id_leakTestBtn);
        leakTestBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                releaseBtn();
            }
        });
        startBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                startBtn();
            }
        });
        stopBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                stopBtn();
            }
        });
    }

    private void startBtn() {
        if (waveLineView != null) {
            waveLineView.setVisibility(Component.VISIBLE);
            waveLineView.startAnim();
            MyApplication.setWaveStart(true);
        } else {
            HiLog.info(label, "waveLineView   null ");
            stopBtn.setText("I am stop Button");
        }
    }

    private void stopBtn() {
        if (waveLineView.isRunning()) {
            waveLineView.stopAnim();
        }
        MyApplication.setWaveStart(false);
        MyApplication.setOrientationChanged(false);
    }

    private void releaseBtn() {
        titlebardirLayout.setVisibility(Component.VISIBLE);
        if (waveLineView.isIsStart() == true) {
            waveLineView.release();
        }
        waveLineView.setVisibility(Component.INVISIBLE);
        MyApplication.setWaveStart(false);
        MyApplication.setOrientationChanged(false);
        LeakAbilitySlice leakAbilitySlice = new LeakAbilitySlice();
        Intent intent1 = new Intent();
        present(leakAbilitySlice,intent1);
        onStop();
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        MyApplication.setOrientationChanged(true);

        HiLog.info(label,"waveLineView21111");
        int height = getDisplayHeightInPx(getContext());
        int width = getDisplayWidthInPx(getContext());
        HiLog.info(label,"" + width + ",  " + height);
        if (waveLineView != null) {
            waveLineView.setWidth(width);
            if (displayOrientation == LANDSCAPE) {
                HiLog.warn(label,"======MainAbility onOrientationChanged  ");
                if (waveLineView.isRunning()) {
                    waveLineView.stopAnim();
                }
                if (MyApplication.isWaveStart() == true) {
                    waveLineView.startAnim();
                }

                if (waveLineView.isRunning() == false) {
                    waveLineView.startAnim();
                    waveLineView.stopAnim();
                }
            }

            else if (displayOrientation == PORTRAIT) {
                HiLog.warn(label,"======MainAbility onOrientationChanged  111111");
                if (waveLineView.isRunning()) {
                    waveLineView.stopAnim();
                }
                if (MyApplication.isWaveStart() == true) {
                    waveLineView.startAnim();
                }

                if (waveLineView.isRunning() == false) {
                    waveLineView.startAnim();
                    waveLineView.stopAnim();
                }

            }
        }

    }

    /**
     * 获取屏幕宽度
     *
     * @param context

     * @return 屏幕宽度
     */
    public static int getDisplayWidthInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().width;
    }

    /**
     * 获取屏幕高度，不包含状态栏的高度
     *
     * @param context

     * @return 屏幕高度，不包含状态栏的高度
     */
    public static int getDisplayHeightInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().height;
    }


    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(label,"onStop");
        MyApplication.setWaveStart(false);
        MyApplication.setOrientationChanged(false);
        terminate();
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        HiLog.info(label,"onAbilityResult");
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(label,"onBackground");
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(label,"onInactive");
    }

    @Override
    public void onActive() {
        super.onActive();
        HiLog.info(label,"onActive");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLog.info(label,"onForeground");
    }
}
